package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArticleListener {

    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = "article_down_or_up")
    public void downOrUp(String jsonData){
        Map map = JSON.parseObject(jsonData, Map.class);
        log.info("app端文章上下架操作监听到消息：{}",map);
        apArticleService.downOrUp(map);
    }

    @KafkaListener(topics = "article_comment")
    public void comment(String jsonData){
        Map map = JSON.parseObject(jsonData, Map.class);
        log.info("app端文章评论操作监听到消息：{}",map);
        apArticleService.comment(map);
    }

    /**
     * 点赞/取消点赞
     *
     * @param jsonData
     */
    @KafkaListener(topics = "article_behavior_like")
    public void like(String jsonData){
        Map map = JSON.parseObject(jsonData, Map.class);
        log.info("app端文章点赞操作监听到消息：{}",map);
        apArticleService.like(map);
    }

    /**
     * 用户阅读
     *
     * @param jsonData
     */
    @KafkaListener(topics = "article_behavior_read")
    public void read(String jsonData){
        Map map = JSON.parseObject(jsonData, Map.class);
        log.info("app端文章阅读操作监听到消息：{}",map);
        apArticleService.read(map);
    }
}
